package com.bsj.media.init.check;

import com.bsj.springQueue.gatewayHandle.ChannelInfo;
import com.bsj.springQueue.gatewayHandle.TimeoutRun;
import io.netty.channel.ChannelHandlerContext;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yinhao
 * @version 1.0
 * @description 管道检测线程
 * @date 2024/8/22
 */
public class NowChannelCheckThread {
    private Thread thread;
    private TimeoutRun timeOutRun;
    private long expireTime = 1800000L;
    private long goRunTime = 60000L;
    volatile boolean isRunning = true;
    private Map<ChannelHandlerContext, ChannelInfo> map = new ConcurrentHashMap();

    public NowChannelCheckThread() {
    }

    public NowChannelCheckThread(long goRunTimeNow) {
        this.goRunTime = goRunTimeNow;
    }

    public NowChannelCheckThread(long expireTimeNow, long goRunTimeNow) {
        this.expireTime = expireTimeNow;
        this.goRunTime = goRunTimeNow;
    }

    public void push(ChannelHandlerContext channelHandlerContext) {
        ChannelInfo channelInfo = this.map.get(channelHandlerContext);
        if (channelInfo == null) {
            this.add(channelHandlerContext);
        } else {
            channelInfo.initTime();
        }

    }

    public void add(ChannelHandlerContext channelHandlerContext) {
        ChannelInfo channelInfo = new ChannelInfo();
        channelInfo.initTime();
        this.map.put(channelHandlerContext, channelInfo);
    }

    public void delChannelHandlerContext(ChannelHandlerContext channelHandlerContext) {
        this.map.remove(channelHandlerContext);
        this.timeOutRun.delChannel(channelHandlerContext);
    }

    public void start(TimeoutRun now) {
        this.timeOutRun = now;
        this.thread = new Thread(new Runnable() {
            public void run() {
                while(NowChannelCheckThread.this.isRunning) {
                    long t1 = System.currentTimeMillis();
                    NowChannelCheckThread.this.doRun();
                    long t = System.currentTimeMillis() - t1;
                    long timeGo = NowChannelCheckThread.this.goRunTime - t;
                    timeGo = timeGo > 0L ? timeGo : 0L;

                    try {
                        Thread.sleep(timeGo);
                    } catch (Exception var8) {
                    }
                }

            }
        });
        this.thread.start();
    }

    public void stop() {
        this.isRunning = false;
        this.thread.interrupt();
    }

    private void doRun() {
        if (this.map != null && !this.map.isEmpty()) {
            Set<ChannelHandlerContext> delSet = new HashSet();
            long now = System.currentTimeMillis();
            Iterator var4 = this.map.entrySet().iterator();

            while(var4.hasNext()) {
                Map.Entry<ChannelHandlerContext, ChannelInfo> entry = (Map.Entry)var4.next();
                ChannelInfo value = entry.getValue();
                if (value == null) {
                    delSet.add(entry.getKey());
                } else {
                    //不进行过期时间检测
//                    long time = value.getTime();
//                    long t = now - time;
//                    if (t > this.expireTime) {
//                        delSet.add(entry.getKey());
//                    }
                }
            }

            if (delSet != null && !delSet.isEmpty()) {
                var4 = delSet.iterator();

                while(var4.hasNext()) {
                    ChannelHandlerContext context = (ChannelHandlerContext)var4.next();
                    this.delChannelHandlerContext(context);
                }
            }
        }

    }
}
